package com.spring.springboot.controller;

import java.sql.*;

import static java.lang.Class.forName;

/**
 * @author lking
 * @date 2019/11/29 20:50
 **/
public class test {


    public static void main(String[] args) throws SQLException {


        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        //连接MySql数据库，用户名和密码都是root
        String url = "jdbc:mysql://222.22.91.81:3306/lking";
        String username = "root";
        String password = "123456";
        Connection con =null;
        try {
             con =DriverManager.getConnection(url, username, password);
        } catch (SQLException se) {
            System.out.println("数据库连接失败！");
            se.printStackTrace();
        }
        Statement stmt = con.createStatement() ;
        ResultSet rs = stmt.executeQuery("SELECT * FROM user");

        while(rs.next()){
            String id = rs.getString("id") ;
            String name = rs.getString(1) ; // 此方法比较高效
            System.out.println(id +name);
        }

        if (rs != null) {   // 关闭记录集
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {   // 关闭声明
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {  // 关闭连接对象
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}